Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.90 KB | None | 0 0
  1. <?php
  2.  
  3.     class Database
  4.     {
  5.         private static $singleton;
  6.  
  7.         private $mysqli;
  8.         private $result;
  9.  
  10.         private $queryCount;
  11.         private $fetchCount;
  12.         private $totalQueryTime;
  13.  
  14.         /* @var Database */
  15.         public static function GetInstance()
  16.         {
  17.             if (!Database::$singleton)
  18.                 Database::$singleton = new Database();
  19.  
  20.             return Database::$singleton;
  21.         }
  22.  
  23.         public function __construct()
  24.         {
  25.             $this->mysqli = mysqli_connect("localhost", "root", "vertrigo", "cardgame");
  26.             $this->queryCount = 0;
  27.             $this->fetchCount = 0;
  28.             $this->totalQueryTime = 0;
  29.  
  30.             if (!$this->mysqli)
  31.                 $this->TraceError("Failed to connect to server");
  32.         }
  33.  
  34.         public function __destruct()
  35.         {
  36.             mysqli_close($this->mysqli);
  37.         }
  38.  
  39.         public function Filter($q, $stripXSS = true)
  40.         {
  41.             return $stripXSS ? htmlspecialchars(mysqli_real_escape_string($this->mysqli, $q)) : mysqli_real_escape_string($this->mysqli, $q);
  42.         }
  43.  
  44.         /*
  45.          * Execute query to database.
  46.          *
  47.          * Query format:
  48.          *  SELECT * FROM table WHERE id = '%1%'
  49.          * Where %1 is argument index from $args array.
  50.          *
  51.          * $args are automatically escaped.
  52.          */
  53.         public function Query($q, $args)
  54.         {
  55.             for ($i = 0; $i < sizeof($args); $i++)
  56.                 $q = str_replace("%$i%", $this->Filter($args[$i]), $q);
  57.  
  58.             $start = microtime(true);
  59.             $this->result = mysqli_query($this->mysqli, $q);
  60.             $this->totalQueryTime += microtime(true) - $start;
  61.  
  62.             if (!$this->result)
  63.                 $this->TraceError("Failed to execute query $q");
  64.  
  65.             $this->queryCount++;
  66.  
  67.             return true;
  68.         }
  69.  
  70.         protected function TraceError($error)
  71.         {
  72.             if (error_reporting() & E_ERROR) {
  73.                 $backtrace = debug_backtrace();
  74.  
  75.                 echo "<div style='background-color: #FF9999; padding: 15px; border: 1px solid #FF3333; color: black'>";
  76.                 echo "<b>Database trace:</b><br><br>";
  77.                 echo "An error in database is occured:<br>";
  78.                 echo "Error text: $error<br>";
  79.                 echo "mysqli last error: " . mysqli_error($this->mysqli) . "<br><br>";
  80.                 echo "Backtrace:<br><br>";
  81.                 echo "<ul>";
  82.  
  83.                 foreach ($backtrace as $trace)
  84.                     echo "<li><b>$trace[class]$trace[type]$trace[function] in file $trace[file] at line $trace[line]</b></li>";
  85.  
  86.                 echo "</ul>";
  87.                 echo "</div>";
  88.             }
  89.         }
  90.  
  91.         public static function TraceInfo()
  92.         {
  93.             if (error_reporting() & E_ERROR) {
  94.                 $instance = Database::GetInstance();
  95.  
  96.                 echo "Database debug info:<br><br>";
  97.                 echo "Query count: $instance->queryCount<br>";
  98.                 echo "Fetch count: $instance->fetchCount<br>";
  99.                 echo "Total query time: $instance->totalQueryTime ms.";
  100.             }
  101.         }
  102.  
  103.         public function Fetch($q, $args)
  104.         {
  105.             if ($this->Query($q, $args)) {
  106.                 $ret = array();
  107.  
  108.                 while ($arr = mysqli_fetch_array($this->result))
  109.                     $ret[] = $arr;
  110.  
  111.                 $this->fetchCount++;
  112.  
  113.                 return $ret;
  114.             }
  115.  
  116.             return array();
  117.         }
  118.  
  119.         public function GetInsertID()
  120.         {
  121.             return mysqli_insert_id($this->mysqli);
  122.         }
  123.  
  124.         public function GetRowCount()
  125.         {
  126.             return mysqli_num_rows($this->result);
  127.         }
  128.  
  129.         public function GetAffectedRows()
  130.         {
  131.             return mysqli_affected_rows($this->mysqli);
  132.         }
  133.  
  134.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement